//
//  GXRegisterViewController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/26.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXRegisterViewController.h"
#import "GXMainTabBarController.h"
@interface GXRegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *deletebtnTop;

@end

@implementation GXRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.deletebtnTop.constant = Height_StatusBar+10;
    
}
- (IBAction)pressCodeBtn:(UIButton *)sender {
    if (![self checkPhoneNumberValue]) {
        return ;
    }
    NSDictionary * jsonStr = [NSDictionary dictionary];
    jsonStr = @{@"api":@"YCS_USER_ISEXIST",
                @"parameters":@{
                        @"phone":self.phoneNumberTF.text
                        }
                };
    [HttpHelper Post:kBaseURL parameters:jsonStr success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"cod"] isEqualToString:@"001"]) {
            [SVProgressHUD showErrorWithStatus:@"请输入6位验证码"];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObject[@"describa"]];
        }
    } failure:^(NSError *error) {
        
    }];
    
}
- (IBAction)pressDeleteBtn:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)pressLoginBtn:(UIButton *)sender {
    if (![self checkTextFieldValue]) {
        return;
    }
    [self viewResignResponder];
//    [MBProgressHUD showMessage:@"注册中..."];
//    NSDictionary * jsonStr = [NSDictionary dictionary];
//    jsonStr = @{@"api":@"CS_USER_INSERTUSER",
//                @"parameters":@{
//                        @"phone":self.phoneNumberTF.text
//                        @"password":
//                        }
//                };
//    [HttpHelper Post:kBaseURL parameters:jsonStr success:^(id responseObject) {
//        [MBProgressHUD hideHUD];
//        if ([responseObject[@"cod"] isEqualToString:@"001"]) {
//            [MBProgressHUD showSuccess:@"注册成功,登录中" toView:self.view];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                GXMainTabBarController * mainVC = [[GXMainTabBarController alloc]init];
//                UIWindow * mainWindow = [UIApplication sharedApplication].keyWindow;
//                mainVC.selectedIndex = 1;
//                mainWindow.rootViewController = mainVC;
//            });
//        }else{
//                [MBProgressHUD showError:responseObject[@"describa"]];
//        }
//    } failure:^(NSError *error) {
//        [MBProgressHUD hideHUD];
//    }];
}


# pragma mark 输入信息判断
- (BOOL)checkTextFieldValue
{
    if ([NSString isBlankString:self.passwordTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"密码不能为空"];
        return NO;
    }
    if ([NSString isBlankString:self.codeTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"验证码不能为空"];
        return NO;
    }
    if (self.codeTF.text.length != 6) {
        [SVProgressHUD showErrorWithStatus:@"请注意验证码的长度为6"];
        return NO;
    }
    return YES;
}
- (BOOL)checkPhoneNumberValue
{
    if ([NSString isBlankString:self.phoneNumberTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
        return NO;
    }
    if (self.phoneNumberTF.text.length !=11) {
        [SVProgressHUD showErrorWithStatus:@"输入手机号不正确"];
        return NO;
    }
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self viewResignResponder];
}
- (void)viewResignResponder
{
    [self.passwordTF resignFirstResponder];
    [self.phoneNumberTF resignFirstResponder];
    [self.codeTF resignFirstResponder];
}
@end
